﻿namespace ImageProcessing.ImageProcessor
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class IMG_OrderFilters
    {
        /// <summary>
        /// TODO: Update summary.
        /// </summary>
        /// <param name="orig_img">origional image.</param>
        /// <param name="window_length">window size.</param>
        /// <param name="t">type of filter.</param>
        /// <returns>Edited image.</returns>
        public static Entities.IMG_Image IMG_FilterEffect(Entities.IMG_Image orig_img, int window_length, IMG_ColorSorting.Img_SortingType t)
        {
            try
            {
                Entities.IMG_Image window = new Entities.IMG_Image(window_length, window_length);
                Entities.IMG_Image returnedIMG = new Entities.IMG_Image(orig_img.Width, orig_img.Height);
                for (int dim_I = 0; dim_I < orig_img.Height; dim_I++)
                {
                    for (int dim_J = 0; dim_J < orig_img.Width; dim_J++)
                    {
                        for (int msk_I = -1 * window_length / 2; msk_I <= window_length / 2; msk_I++)
                        {
                            for (int msk_J = -1 * window_length / 2; msk_J <= window_length / 2; msk_J++)
                            {
                                int newXindex = dim_I + msk_I;
                                int newYindex = dim_J + msk_J;
                                if (newXindex >= 0 && newXindex < orig_img.Height && newYindex >= 0 && newYindex < orig_img.Width)
                                {
                                    window[msk_I + (window_length / 2), msk_J + (window_length / 2)] = orig_img[newXindex, newYindex];
                                }
                                else
                                {
                                    window[msk_I + (window_length / 2), msk_J + (window_length / 2)] = null;
                                }
                            }
                        }

                        switch (t)
                        {
                            case IMG_ColorSorting.Img_SortingType.Min:
                                returnedIMG[dim_I, dim_J] = IMG_ColorSorting.Img_BitMixVal(window, IMG_ColorSorting.Img_SortingType.Min);
                                break;
                            case IMG_ColorSorting.Img_SortingType.Max:
                                returnedIMG[dim_I, dim_J] = IMG_ColorSorting.Img_BitMixVal(window, IMG_ColorSorting.Img_SortingType.Max);
                                break;
                            case IMG_ColorSorting.Img_SortingType.Midpoint:
                                returnedIMG[dim_I, dim_J] = IMG_ColorSorting.Img_BitMixVal(window, IMG_ColorSorting.Img_SortingType.Midpoint);
                                break;
                            case IMG_ColorSorting.Img_SortingType.Median:
                                returnedIMG[dim_I, dim_J] = IMG_ColorSorting.Img_BitMixVal(window, IMG_ColorSorting.Img_SortingType.Median);
                                break;
                        }
                    }
                }

                return returnedIMG;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                throw;
            }
        }
    }
}